package booking;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//Utility class to maintain booking page
//developed by Lidiya  Chhailo
public class bookUtil 
{
    private String driver;
    private String url;
    private String user;
    private String pswd;
    private Connection conn;
  
    
	
	
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) 
	{
		System.out.println("Driver:"+driver);
		this.driver = driver;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url)
	{
		System.out.println("Connection url:"+url);
		this.url = url;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) 
	{
		System.out.println("User name :"+user);
		this.user = user;
	}
	public String getPswd() {
		return pswd;
	}
	public void setPswd(String pswd) {
		System.out.println("Password :"+pswd);
		this.pswd = pswd;
	}
	public Connection getCon() {
		return conn;
	}
	public void setCon(Connection con) {
		this.conn = con;
	}
	
	public void closeDBConnection()
	{
		if(getCon() != null )
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
    
	 private void initDataBaseConnection()
	    {
	    	
			 try
			 {
				Class.forName(getDriver());
				setCon( DriverManager.getConnection(getUrl(),getUser(),getPswd()));
			 } 
			 catch (SQLException e)
			 {
					// TODO Auto-generated catch block
					e.printStackTrace();		 
			 } 
			 catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			  }
	    }
	 
	 public  ResultSet getDbRecord(String custId) throws IOException 
		{
			
			Statement stmt;
			String sql = "SELECT b.bookingid,bd.description,bd.tripstart,bd.tripend,bd.destination "+
             "FROM bookings b,bookingdetails bd "+
             "where b.bookingid = bd.bookingid and b.customerid = '"+ custId+ "'";
			System.out.println(sql);
			ResultSet rs = null;		
			try
			{
			   initDataBaseConnection();
			   stmt = conn.createStatement();
			   rs = stmt.executeQuery(sql);		   
			
			}
			catch(Exception e)
			{
				e.printStackTrace();
				System.out.println("Data base exception");
			}
			return rs;
		}
		
    
}
